package cn.zjweu.servlet;

import cn.zjweu.domain.Manager;
import cn.zjweu.domain.MessageInfo;
import cn.zjweu.domain.ResultInfo;
import cn.zjweu.domain.User;
import cn.zjweu.service.ManagerService;
import cn.zjweu.service.UserService;
import cn.zjweu.service.impl.ManagerServiceImpl;
import cn.zjweu.service.impl.UserServiceImpl;
import cn.zjweu.utils.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/*Designed By YeJianFeng-2022b32057*/
@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();
    private ManagerService managerService = new ManagerServiceImpl();

    public void findUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int userId = 0;
        ResultInfo info = new ResultInfo();
        User user = null;
        try {
            userId = Integer.parseInt(request.getParameter("userId"));
            user = userService.findUserById(userId);

            info.setFlag(MessageInfo.SUCCESS);
            info.setData(user);
            info.setInfoMsg("用户ID查找成功！");
        } catch (Exception e) {
            System.out.println("用户ID查找失败");

            info.setFlag(MessageInfo.FAIL);
            info.setData(userId);
            info.setInfoMsg("用户ID查找失败！");
        } finally {
            writeValue(info, response);
        }
    }

    public void editUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userId = request.getParameter("userId");
        String username = request.getParameter("username");
        String trueName = request.getParameter("trueName");
        String telephone = request.getParameter("telephone");
        String city = request.getParameter("city");
        String address = request.getParameter("address");

        ResultInfo info = new ResultInfo();
        User user = null;
        try {
            user = new User(Integer.parseInt(userId), username, null, trueName, telephone, city, address);
            if (userService.editUser(user)) {
                System.out.println("修改成功！");

                info.setFlag(MessageInfo.SUCCESS);
                info.setData(user);
                info.setInfoMsg("修改成功！");
            } else {
                System.out.println("修改失败");

                info.setFlag(MessageInfo.FAIL);
                info.setData(user);
                info.setInfoMsg("修改失败！");
            }
        } catch (Exception e) {
            System.out.println("用户修改失败");

            info.setFlag(MessageInfo.FAIL);
            info.setData(username);
            info.setInfoMsg("用户修改错误！");
        } finally {
            writeValue(info, response);
        }
    }

    public void register(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String trueName = request.getParameter("trueName");
        String telephone = request.getParameter("telephone");
        String city = request.getParameter("city");
        String address = request.getParameter("address");

        ResultInfo info = new ResultInfo();

        User user = userService.findUsername(username);
        if (user == null) {
            System.out.println("未找到该用户，可以注册");

            //正则手机号判断》》》》》
            if (telephone.length() != MessageInfo.TELEPHONE_LENGTH) {
                info.setFlag(MessageInfo.FAIL);
                info.setData(telephone);
                info.setInfoMsg("注册失败！手机号码输入有误！");
                writeValue(info, response);
                return;
            }

            if (userService.saveUser(username, password, trueName, telephone, city, address)) {
                System.out.println("注册成功！");
                //跳转成功页面
                info.setFlag(MessageInfo.SUCCESS);
                info.setData("login.jsp");
                info.setInfoMsg("注册成功！");
            } else {
                info.setFlag(MessageInfo.FAIL);
                info.setData(username);
                info.setInfoMsg("注册失败！");
            }
        } else {
            System.out.println(user);
            info.setFlag(MessageInfo.FAIL);
            info.setData(user);
            info.setInfoMsg("用户名已存在！");
        }
        writeValue(info, response);
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = userService.findUserByNameAndPWD(username, password);
        if (user != null) {
            System.out.println("登录成功！");
            info.setFlag(MessageInfo.SUCCESS);
            info.setData("index.jsp");
            info.setInfoMsg("登录成功！");
            HttpSession session = request.getSession();
            session.setAttribute("username", username);
            session.setAttribute("userId", user.getUserId());
        } else {
            System.out.println("登录失败!");
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("登录失败！用户名或密码错误！");
        }
        writeValue(info, response);
    }

    public void exit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().invalidate();
        response.sendRedirect(request.getContextPath() + "/index.jsp");
    }

    /*管理员登录*/
    public void loginAd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Manager manager = managerService.findAdByNameAndPWD(username, password);
        if (manager != null) {
            System.out.println("登录成功！");
            info.setFlag(MessageInfo.SUCCESS);
            info.setData("administer.jsp");
            info.setInfoMsg("登录成功！");
            HttpSession session = request.getSession();
            session.setAttribute("admin", username);
        } else {
            System.out.println("登录失败!");
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("登录失败！账号或密码错误！");
        }
        writeValue(info, response);
    }

    public void findAllUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();

        try {
            List<User> userList = userService.findAllUser();
            info.setFlag(MessageInfo.SUCCESS);
            info.setData(userList);
            info.setInfoMsg("查找成功！");
        } catch (Exception e) {
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("查找失败！");
        } finally {
            writeValue(info, response);
        }
    }

    public void resetPWD(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        int userId = 0;
        try {
            userId = Integer.parseInt(request.getParameter("userId"));

            userService.resetPWD(userId);
            System.out.println("重置成功");

            info.setFlag(MessageInfo.SUCCESS);
            info.setData(userId);
            info.setInfoMsg("重置成功！");
        } catch (Exception e) {
            System.out.println("重置失败");

            info.setFlag(MessageInfo.FAIL);
            info.setData(userId);
            info.setInfoMsg("重置失败！");
        } finally {
            writeValue(info, response);
        }
    }

    public void delUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        int userId = 0;
        try {
            userId = Integer.parseInt(request.getParameter("userId"));

            userService.delUserById(userId);
            System.out.println("删除成功");

            info.setFlag(MessageInfo.SUCCESS);
            info.setData(userId);
            info.setInfoMsg("删除成功！");
        } catch (Exception e) {
            System.out.println("删除失败");

            info.setFlag(MessageInfo.FAIL);
            info.setData(userId);
            info.setInfoMsg("删除失败！");
        } finally {
            writeValue(info, response);
        }
    }
}
